package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.pojo.CacMinorItem;
import com.csun.cmny.provider.service.CacMinorItemService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/*
 * 评估小项管理——机构额外小项
 * */
@RestController
@RequestMapping("/CacMinorItem")
public class CacMinorItemController extends BaseController {
    @Autowired
    private CacMinorItemService cacMinorItemService;

    /*
     * 这里的认证和当前登录的用户注解先预留，等完善了再进行修改
     * 1.判断该用户是否属于该机构人员
     * */
    //添加单个小项
    @RequestMapping(value = "/addCacMinorItem", method = RequestMethod.POST)
    public Wrapper<CacMinorItem> addCacType(CacMinorItem cacMinorItem) {
        //校验
        if (cacMinorItem.getName() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项名称不能为空", null);
        }
        if (cacMinorItem.getBigItemId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        cacMinorItem.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        CacMinorItem cacMinorItem1 = new CacMinorItem();
        cacMinorItem1.setBigItemId(cacMinorItem.getBigItemId());
        cacMinorItem1.setName(cacMinorItem.getName());
        cacMinorItem1.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        //根据小项名称和大项id判断该小项是否在该大项中已经存在
        CacMinorItem cacMinorItem2 = cacMinorItemService.selectByCmi(cacMinorItem1);
        if (cacMinorItem2 != null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该小项已经在该大项中存在", null);
        }
        /*判断该小项所属大项的类别(type)
         * 0-表示要计算的项，1-表示是类别总分，2-表示是类别的等级
         * 如果type是0，分数一定不为空；如果type是1，分数和描述一定为空；如果type是2，描述一定不为空
         * 此暂时未实现
         * */

        int n = cacMinorItemService.insert(cacMinorItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败", cacMinorItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功", cacMinorItem);
    }

    //根据小项id删除单个小项
    @RequestMapping(value = "/delCacMinorItem", method = RequestMethod.POST)
    public Wrapper delCacType(Integer minorItemId) {
        if (minorItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项id不能为空", null);
        }
        //获取当前用户的机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        //判断当前登录用户是否是该机构人员

        //删除
        int n = cacMinorItemService.delById(minorItemId);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败", n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功", n);
    }

    //修改单个类别
    @RequestMapping(value = "/updateCacMinorItem", method = RequestMethod.POST)
    public Wrapper<CacMinorItem> updateCacType(CacMinorItem cacMinorItem) {
        if (cacMinorItem.getMinorItemId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项id不能为空", null);
        }
        //判断当前登录用户是否是该机构人员

        /*判断该小项所属大项的类别(type)
         * 0-表示要计算的项，1-表示是类别总分，2-表示是类别的等级
         * 如果type是0，分数一定不为空；如果type是1，分数和描述一定为空；如果type是2，描述一定不为空
         * 此暂时未实现
         * */

        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        cacMinorItem.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        CacMinorItem cacMinorItem1 = new CacMinorItem();
        cacMinorItem1.setBigItemId(cacMinorItem.getBigItemId());
        cacMinorItem1.setName(cacMinorItem.getName());
        cacMinorItem1.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        //根据小项名称和大项id判断该小项是否在该大项中已经存在
        CacMinorItem cacMinorItem2 = cacMinorItemService.selectByCmi(cacMinorItem1);
        if (cacMinorItem2 != null && !cacMinorItem2.getMinorItemId().equals(cacMinorItem.getMinorItemId())) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "该小项已经在该大项中存在", cacMinorItem2);
        }

        int n = cacMinorItemService.update(cacMinorItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "修改失败", cacMinorItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "修改成功", cacMinorItem);
    }

    //查询单个小项
    @RequestMapping(value = "/selectCmiById", method = RequestMethod.POST)
    public Wrapper selectCbiById(Integer minorItemId) {
        if (minorItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项id不能为空", null);
        }
        CacMinorItem cacMinorItem = cacMinorItemService.selectCmiById(minorItemId);
        if (cacMinorItem == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", minorItemId);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "查询成功", cacMinorItem);
    }
}
